﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using Common.Protocol;

namespace PPTControl
{
    public partial class LoginForm : Form
    {
        private IProxy _proxy = null;

        public int UID
        {
            get;
            set;
        }

        public LoginForm(IProxy proxy)
        {
            InitializeComponent();
            UID = 0;
            _proxy = proxy;
        }
        public LoginReq LoginInfo
        {
            get;
            set;
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textBoxUserName.Text.Trim().Length == 0)
            {
                MessageBox.Show("用户名不能为空！");
                return;
            }

            if (textBoxPassword.Text.Trim().Length == 0)
            {
                MessageBox.Show("密码不能为空！");
                return;
            }

            LoginReq reqCmd = new LoginReq(textBoxUserName.Text.Trim(), textBoxPassword.Text.Trim(), LoginReq.SOURCETYPE_PPT);

            try
            {
                if (textBoxDebugServer.Text != null && textBoxDebugServer.Text.Trim().Length != 0)
                {
                    _proxy.DebugServer = textBoxDebugServer.Text;
                }
                
                LoginResp respCmd = _proxy.Login(reqCmd);

                if (respCmd.AckCode == LoginResp.LOGINACK_SUCCESS)
                {
                    UID = respCmd.Uid;
                    LoginInfo = reqCmd;
                }
                else
                {
                    MessageBox.Show("登录失败！");
                    DialogResult = DialogResult.None;
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                DialogResult = DialogResult.None;
                return;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {

        }
    }
}
